<template>
  <el-card class="flex justify-end">
    <el-date-picker class=" mt-4 mr-4" v-model="dateValue" type="daterange" range-separator="To"
      start-placeholder="2022-01-01" end-placeholder="2023-12-31" @change="dateChange" value-format="YYYY-MM-DD" />
    <div class="mt-4">
      <el-input prefix-icon="Search" clearable v-model="searchVal" placeholder="输入用户编号检索" />
    </div>
  </el-card>

  <el-card class="mt10">
    <el-table ref="table" :data="tableData.list" :header-cell-style="{ backgroundColor: '#ecf5ff' }" border stripe
      style="width: 100%">
      <el-table-column width="100" prop="id">
        <template #default="scope">
          <el-image style="width: 60px;height: 60px;" :src="'api/uploads/' + scope.row.avatar" />
        </template>
      </el-table-column>
      <el-table-column label="用户编号" width="100" prop="id">
      </el-table-column>
      <el-table-column label="注册日期" sortable="true" width="200">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-icon>
              <timer />
            </el-icon>
            <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="最近一次修改" sortable="true" prop="updateTime" width="200">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-icon>
              <timer />
            </el-icon>
            <span style="margin-left: 10px">{{ scope.row.updateTime }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="账号" width="130" prop="userName">
      </el-table-column>
      <el-table-column label="名称" width="130" prop="nickName">
      </el-table-column>
      <el-table-column label="性别" width="80" prop="sex">
        <template #default="scope">
          {{ showSex(scope.row.sex) }}
        </template>
      </el-table-column>
      <el-table-column label="邮箱" width="180" prop="email">
      </el-table-column>
      <el-table-column label="用户类型" width="120">
        <template #default="scope">
          <el-tag :type="scope.row.status === '1' ? 'error' : 'success'" disable-transitions>{{
            showUserType(scope.row.userType)
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="70">
        <template #default="scope">
          <el-tag :type="scope.row.status === '1' ? 'error' : 'success'" disable-transitions>{{
            showStatus(scope.row.status)
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot:default="scope">
          <el-icon class="icon-edit" @click="itemEditHanld(scope.row)">
            <Edit />
          </el-icon>
          <el-popconfirm confirm-button-text="确认" cancel-button-text="取消" :icon="InfoFilled" icon-color="#626AEF"
            title="确认停用该账号?" @confirm="DeleteItem(scope.row)">
            <template #reference>
              <el-icon class="icon-dele">
                <Delete />
              </el-icon>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class=" ml-48 mt-4" background v-model:current-page="queryData.currentPage"
      v-model:page-size="queryData.pageSize" :page-sizes="[10, 20, 30, 40]" small="small"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </el-card>
  <el-dialog v-model="dialogVisible" title="更新用户" width="30%" :before-close="handleClose">
    <el-form :model="updateInfo" label-width="120px">
      <el-form-item label="用户编号">
        <el-input disabled v-model="updateInfo.id" />
      </el-form-item>
      <el-form-item label="账号">
        <el-input disabled v-model="updateInfo.userName" />
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="updateInfo.nickName" />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="updateInfo.email" />
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="updateInfo.sex">
          <el-option label="男" value="1" />
          <el-option label="女" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="updateInfo.status">
          <el-option label="正常" value="0" />
          <el-option label="停用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="身份">
        <el-select v-model="updateInfo.userType">
          <el-option label="管理员" value="1" />
          <el-option label="普通用户" value="2" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" class=" bg-blue-500" @click="handleUpdate">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import {
  Edit,
  Delete,
  InfoFilled,
} from '@element-plus/icons-vue'
import { nextTick, onMounted, reactive, ref, watch } from 'vue'
import { options } from './options'
import { userList, deleteUser, updateUser } from '../../../api/modules';
import { useStore } from "vuex";
import { useRouter } from 'vue-router';
import { ElNotification, ElMessageBox } from 'element-plus';
const tableData = reactive({
  list: []
})
const router = useRouter()
const store = useStore()
const table = ref()
const dialogVisible = ref(false)
const total = ref(0)
const loading = ref(false)
const queryData = ref({
  currentPage: 1,
  pageSize: 10,
  id: "",
  start: "",
  end: "",
})
const showSex = (sex) => {
  if (sex === '1') {
    return "男"
  }
  return "女"
}
const dateValue = ref()
const dateChange = () => {
  if (dateValue.value == undefined) {
    queryData.value.start = ''
    queryData.value.end = ''
  } else {
    queryData.value.start = dateValue.value[0];
    queryData.value.end = dateValue.value[1];
  }
  initData();
}
const showStatus = (status) => {
  if (status == '0') {
    return "正常"
  }
  if (status == '1') {
    return "停用"
  }
}
const showUserType = () => {
  return '普通用户'
}
const handleClose = (done) => {
  ElMessageBox.confirm('您确定关闭对话框吗？')
    .then(() => {
      done()
    })
    .catch(() => {
      // catch error
    })
}
const initData = async () => {
  loading.value = true
  let res = await userList(queryData.value)
  total.value = res.data.data.total
  tableData.list = res.data.data.records
  loading.value = false
}
const DeleteItem = async (item) => {
  let res = await deleteUser(item.id)
  if (res.data.data > 0) {
    ElNotification.success("停用成功")
    let res1 = await getDrugList(queryData.value)
    tableData.list = res1.data.data.records
  } else {
    ElNotification.error("操作失败")
  }
}
const updateInfo = reactive({
  id: "",
  email: "",
  status: "",
  userType: "",
  userName: "",
  nickName: "",
  sex: ""
})
const itemEditHanld = (item) => {
  dialogVisible.value = true;
  updateInfo.id = item.id
  updateInfo.email = item.email
  updateInfo.status = item.status
  updateInfo.userType = item.userType
  updateInfo.userName = item.userName
  updateInfo.nickName = item.nickName
  updateInfo.sex = item.sex
}
const handleUpdate = async () => {
  let res = await updateUser(updateInfo)
  if (res.data.data > 0) {
    ElNotification.success('更新成功')
    dialogVisible.value = false;
    initData()
  }
}
const handleSizeChange = (val) => {
  queryData.value.pageSize = val
  initData(queryData.value)
}
const handleCurrentChange = (val) => {
  queryData.value.currentPage = val
  initData(queryData.value)
}
const searchVal = ref('')
watch(searchVal, (newVal, oldVal) => {
  queryData.value.id = newVal
  initData()
})
onMounted(() => {
  initData()
})
</script>

<style lang="scss" scoped>
.mt-4 {
  float: right;
  text-align: right;
  margin-bottom: 20px;
}

.demo-form-inline {}

.dragClass {
  background: rgba($color: #41c21a, $alpha: 0.5) !important;
}

// 停靠
.ghostClass {
  background: rgba($color: #6cacf5, $alpha: 0.5) !important;
}

// 选择
.chosenClass:hover>td {
  background: rgba($color: #f56c6c, $alpha: 0.5) !important;
}

.icon-view {
  font-size: 20px;
  color: #673ab7;
  margin: 0 10px;
}

.icon-edit {
  font-size: 20px;
  color: #2f60c2;
  margin: 0 10px;
}

.icon-dele {
  font-size: 20px;
  color: #ff5722;
}

.btn-color {
  background: #fff;
  color: #4c60cc;
}

.expand-title {
  padding: 10px;
  color: #919399;
  font-weight: 600;
}
</style>
